<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/cate/${goods.categories[1].id}`">{{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/cate/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales :goods="goods"></GoodsSales>
        </div>
        <div class="spec">
          <GoodsName :goods="goods"></GoodsName>
          <GoodsSku @change="getSku" :goods="goods"></GoodsSku>
          <XtxNumbox label="数量" :max="goods.inventory" v-model="number"></XtxNumbox>
          <XtxButton @click="addCart" style="margin-top: 20px;" type="primary">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"></GoodsRelevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- type值为1代表24小时热销榜，type值为2代表周热销榜，type值为3代表总热销榜 -->
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './components/goods-sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn'
import { ref, watch, nextTick, provide } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/product'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup() {
    const store = useStore()
    const route = useRoute()
    const goods = useGoods()
    // 获取当前选中的sku数据
    const currentSku = ref(null)
    // 获取选中的sku结果
    const getSku = (sku) => {
      // 把sku结果存放在goods数据中
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
        currentSku.value = sku
      } else {
        currentSku.value = null
      }
    }
    watch(() => route.params.id, () => {
      number.value = 1
    })
    // 选择的商品数据
    const number = ref(1)
    // 通过provide提供数据给后代组件使用
    provide('goods', goods)
    // 添加购物车
    const addCart = () => {
      // 判断有没有勾选完整的规格参数
      if (!currentSku.value) {
        return Message({ text: '请选择完整的规格!' })
      }
      if (number.value > goods.value.inventory) {
        return Message({ text: '库存不足' })
      }
      // 组装需要添加的商品数据:id  name picture skuId price nowPrice count attrsText selected stock isEffective
      const { id, name } = goods.value
      const count = number.value
      const { skuId, price: nowPrice, price, specsText: attrsText, inventory: stock } = currentSku.value
      const selected = true
      const isEffective = true
      const picture = goods.value.mainPictures[0]
      // 提交
      store.dispatch('cart/insertCart', { id, skuId, count, name, price, attrsText, selected, stock, nowPrice, isEffective, picture }).then(() => {
        Message({ type: 'success', text: '添加购物车成功' })
      })
    }
    return { goods, getSku, number, addCart }
  }
}
// 获取商品详情
const useGoods = () => {
  // 出现路由地址商品ID发生变化，但是不会重新初始化组件
  const goods = ref(null)
  const route = useRoute()
  watch(() => route.params.id, (newVal) => {
    if (newVal && `/product/${newVal}` === route.path) {
      findGoods(route.params.id).then(data => {
        // 让商品数据为null让后使用v-if的组件可以重新销毁和创建
        goods.value = null
        nextTick(() => {
          goods.value = data.result
        })
      })
    }
  }, { immediate: true })
  return goods
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;

    .goods-tabs {
      min-height: 600px;
      background: #fff;
    }

    .goods-warn {
      min-height: 600px;
      background: #fff;
      margin-top: 20px;
    }
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
</style>